package com.jie.controller;

import com.jie.dao.BlogMapper;
import com.jie.pojo.Blog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/19/14:29
 * @Description:
 */
@Controller
@Slf4j
public class ArchiveShowController {

    @Autowired
    private BlogMapper blogMapper;

    @GetMapping("/archives")
    public String getArchives(Model model){
        List<String> stringList = blogMapper.archiverYear();
        Map<String,List<Blog>> map=new HashMap<>();
        for (String s : stringList) {
            List<Blog> blogs = blogMapper.archiverBlog(s);
            for (Blog byId : blogs) {
                if(byId.getFlag()==0){
                    byId.setFlags("原创");
                }
                if(byId.getFlag()==2){
                    byId.setFlags("转载");
                }
                if(byId.getFlag()==3){
                    byId.setFlags("翻译");
                }
            }
            map.put(s,blogs);
        }
        model.addAttribute("archiveMap",map);
        model.addAttribute("blogCount",blogMapper.selectCount(null));
        return "archives";
    }
}
